<template>
    <div>
        <!-- 搜索栏 -->
        <el-card class="!border-none mb-4" shadow="never">
            <div class="text-xl font-medium mb-[20px]">订阅功能</div>
            <el-form class="mb-[-16px]" label-width="100px">
                <el-form-item label="功能状态">
                    <div>
                        <el-radio-group v-model="formData.status">
                            <el-radio :value="0">开启</el-radio>
                            <el-radio :value="1">关闭</el-radio>
                        </el-radio-group>
                        <div class="form-tips">关闭订阅功能后, 用户端订阅入口不再显示。</div>
                    </div>
                </el-form-item>
                <el-form-item label="时间选项">
                    <div>
                        <el-checkbox
                            v-model="formData.timeline.day"
                            :checked="formData.timeline.day"
                            label="按日"
                            size="large"
                        />
                        <el-checkbox
                            v-model="formData.timeline.month"
                            :checked="formData.timeline.month"
                            label="按月"
                            size="large"
                        />
                        <el-checkbox
                            v-model="formData.timeline.year"
                            :checked="formData.timeline.year"
                            label="按年"
                            size="large"
                        />
                        <el-checkbox
                            v-model="formData.timeline.perpetual"
                            :checked="formData.timeline.perpetual"
                            label="永久"
                            size="large"
                        />
                        <div class="form-tips">用于用户端订阅套餐时界面的时间筛选项。</div>
                    </div>
                </el-form-item>
                <el-form-item>
                    <el-button
                        v-perms="['market:recharge:set_config']"
                        :loading="loading"
                        type="primary"
                        @click="handleSubmit"
                    >保存配置</el-button>
                </el-form-item>
            </el-form>
        </el-card>

        <!-- 表格栏 -->
        <el-card class="!border-none" shadow="never">
            <div class="text-xl font-medium mb-[20px]">订阅套餐</div>
            <el-button type="primary" v-perms="['market:recharge:add']" @click="handleEditor('add')">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                <span>新增</span>
            </el-button>
            <el-table :data="dataLists" :loading="tbLoading" size="large" class="mt-4">
                <el-table-column label="套餐名称" prop="name" min-width="120" />
                <el-table-column label="套餐描述" prop="description" min-width="180" />
                <el-table-column label="套餐定价" prop="price" min-width="130">
                    <template #default="{ row }">
                        ￥{{ row?.min_price }} ~ ￥{{ row?.max_price }}
                    </template>
                </el-table-column>
                <el-table-column label="套餐信息" prop="price" min-width="130">
                    <template #default="{ row }">
                        <p>训练条数: {{ row?.train_num }}</p>
                        <p>智能体数: {{ row?.robot_num }}</p>
                        <p>知识库数: {{ row?.know_num }}</p>
                        <p>MCP数: {{ row?.mcp_num }}</p>
                    </template>
                </el-table-column>
                <el-table-column label="排序" prop="sort" min-width="100" />
                <el-table-column label="状态" prop="is_disable" min-width="100">
                    <template #default="{ row }">
                        <el-tag v-if="row.is_disable === 0">正常</el-tag>
                        <el-tag v-else type="danger">禁用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" prop="create_time" min-width="175" />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['market:recharge:edit']"
                            type="primary"
                            link
                            @click="handleEditor('edit', row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-if="!row.is_builtin"
                            v-perms="['market:recharge:delete']"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <!-- 编辑器 -->
        <editor v-if="showEdit" ref="editorRef" @success="queryLists" @close="showEdit = false" />
    </div>
</template>

<script setup lang="ts">
import feedback from '@/utils/feedback'
import marketSubscribeApi from '@/api/market/subscribe'
import Editor from './editor.vue'

const showEdit = ref(false)
const editorRef = shallowRef<InstanceType<typeof Editor>>()

// 配置表单
const loading = ref(false)
const formData = reactive({
    status: 0,
    timeline: {
        day: false,
        month: false,
        year: false,
        perpetual: false
    }
})

// 列表数据
const tbLoading = ref(false)
const dataLists = ref([])

// 查询列表
const queryLists = async () => {
    tbLoading.value = true
    dataLists.value = await marketSubscribeApi.lists()
        .finally(() => {
            tbLoading.value = false
        })
}

/**
 * 配置查询
 *
 * @author zero
 */
const queryConfig = async () => {
    const data = await marketSubscribeApi.getConfig()
    formData.status = data.status
    formData.timeline = data.timeline
}

/**
 * 配置保存
 *
 * @author zero
 */
const handleSubmit = async (): Promise<void> => {
    loading.value = true
    await marketSubscribeApi.setConfig(formData)
        .finally(() => {
            setTimeout(() => {
                loading.value = false
            }, 500)
        })

    await queryConfig()
    feedback.msgSuccess('保存成功')
}

/**
 * 处理编辑
 *
 * @param {string} type
 * @param {any} row
 * @returns {Promise<void>}
 * @author zero
 */
const handleEditor = async (type: string, row?: any): Promise<void> => {
    showEdit.value = true
    await nextTick()
    editorRef.value?.open(type, row)
}

/**
 * 处理删除
 *
 * @param {number} id
 * @returns {Promise<void>}
 * @author zero
 */
const handleDelete = async (id: number): Promise<void> => {
    feedback.confirm('确定要删除此项数据吗?')
        .then(async () => {
            await marketSubscribeApi.delete(id)
            feedback.msgSuccess('删除成功')
            await queryLists()
        }).catch(() => {})
}

onMounted(async () => {
    await queryLists()
    await queryConfig()
})
</script>
